<?php

namespace App\Console\Commands\OpenShop;

use App\Enums\OpenShop\InvestmentPromotion\ShopManualEnum;
use App\Models\OpenShop\InvestmentPromotion\OpenShopShopManual;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderLogModel;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Kuafu\File\PrivateFileUtil;

class RefreshDisclosure extends Command
{

    const REPORT_PREFIX = 'kuafu_tiger_open_shop_report';

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'open_shop:refresh_all_disclosure';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '重新刷新披露单信息';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        $changesInfo = OpenShopSignOrderLogModel::query()
        ->whereIn('type', [OpenShopSignOrderLogModel::SELECTION_SUBMIT_DISCLOSURE, OpenShopSignOrderLogModel::UPDATED_SUBMIT_DISCLOSURE])
        ->get();
        if ($changesInfo == null) {
            return;
        }
        foreach ($changesInfo as $item) {
            if ($item->changes == '') {
                continue;
            }
            $changesTemp = [
                'old' => [],
                'new' => [],
            ];
            $changes = json_decode($item->changes, true);
            foreach (($changes['old'] ?? []) as $change) {
                $changesTemp['old'][] = str_replace("https://file.51kuafu.com/", '', $change);
            }
            foreach (($changes['new'] ?? []) as $change) {
                $changesTemp['new'][] = str_replace("https://file.51kuafu.com/", '', $change);
            }
            $logModel = OpenShopSignOrderLogModel::find($item->id);
            $logModel->changes = json_encode($changesTemp);
            $logModel->save();
        }
    }


    public function repairShopLocationFile()
    {
        $originalKey = [
            "2f/83/91f60fd4ea9ecf7f1460fc44f5e99c31d8a22ae5a4485344b42062b9e30e.jpeg",
            "ca/31/4928ea1c6a11b5c27442b1f7f2497ea5805ed12ada65aeb984f974a4867a.jpeg",
            "4a/84/bd51c804957ee3fb8f6ec21cb8b529473ee6437e5464fe1c7f90663135dd.jpeg",
            "18/aa/9c6c799178df797f8053688546568367caf7dddc5cc33523c918da9e0fa7.jpeg",
            "39/81/2203a0e5406e917a4194e8567dd182c92c4c754929b76977409b479422ef.jpg",
            "66/be/fc89d5f22ad8ae0520dea1113b1cc50f98e6243df3b43fea5cbaaa223817.jpg",
            "66/be/fc89d5f22ad8ae0520dea1113b1cc50f98e6243df3b43fea5cbaaa223817.jpg",
            "43/8a/6c4f25a052f277998090d2c4555753db557f0c26307b84c889baa0123079.jpg",
            "50/f3/cc178bdb7d99a9086c2a39d9a26c677484d9eeb3a0422683e49247beae5d.jpg",
            "1c/08/c0d090bb7df8f1b09947d8d9784dedb3ada7bdcd0e2d7b70041c4d7c2a01.jpg",
            "2a/39/39e66d9b01b63d41bd1ae09949a4616735c21e8e5b76b69192f5ca9eaea3.jpg",
            "8d/0c/232ac007c91cd5f3e592f6286dc5101fb386a865a218448cb46caef5d817.jpg",
            "bb/f1/0da54511151258d004298a083ee4e245727a98ecb66eb620c80c0bcaea58.jpg",
            "a8/2b/fb4aac02ca23731049b3a9d4c12b6574444d100a496be51545169f70d248.jpg",
            "b8/cb/efdd470878bb1c2b049ce6187008ae7781ae4510f991d32c52e0e93da8e9.jpg",
            "57/1e/7ca55c454b2bd99bb9197a945eae5811f15153eb9b97f7244bdc18a9f12e.jpg",
            "c5/77/c23cb40df42f4e24a69fb5b02e583ce2f195dc5039a95f974ac16b694ea5.jpg",
            "a8/2b/fb4aac02ca23731049b3a9d4c12b6574444d100a496be51545169f70d248.jpg",
            "c5/77/c23cb40df42f4e24a69fb5b02e583ce2f195dc5039a95f974ac16b694ea5.jpg",
            "a8/2b/fb4aac02ca23731049b3a9d4c12b6574444d100a496be51545169f70d248.jpg",
            "2a/d1/a5e9a35680f13c694b068a0dafe479822d52f3e4f0a92848ad475626ab6c.jpg",
            "b0/ac/28b37cbfd7b22586e261cc29d9b690ed1dfdb34f6ace33c27aaf873a2c48.jpg",
            "a2/30/218f52eb9fcf6f3e54ffd62edcf0b059442f519d795a312287fb1bcbe74a.jpg",
            "a2/8a/e6f7383e91c3a613c8f4662d094141086e38b9cd3faf9b9df6c7d40e4608.jpg",
            "b7/0f/02f53b6d5e7b4911fc65e053cec26bd3bcd20f0aaa20376ed333b1f26ebf.jpg",
            "7a/5b/cf0bb3565fc821d3a3d9fd0cb423ea3a097bdcd091b7b4328c219f72983c.jpg",
            "dc/db/45004aca1a9aaa6d0d4ec566d05d00ae38face9b7d49c5ca8096b7afaf09.jpg",
            "bf/06/4714e44a91ca5d753eea3df71273f847748d7200cfbb7f3d033bc07acbd7.jpg",
            "8f/6f/0ee827cf391261abbda466adb7253e397c353c7c5e3a704092808254f285.jpg",
            "29/f3/d15bd903a6b4fc3bc34b2844728cc5655d4ddfbf0b85db0c78548a69f7be.jpg",
            "d1/08/f9bacb4a9e05f22f319ccf19e902d54560358df1e72f74c54babe9531e16.jpg",
            "65/e0/b030696393a1a04faaffa552bf84251fbfd9ff4404dff9957696ac929933.jpg",
            "a7/9a/4a74de518469ddf5595793b30ffc0eed3d9914118b50a8d1f7b643f45a10.jpg",
            "09/7b/eb78962a61f12326735eb5bea40ced469c77de6852807f43176dc20c7f52.jpg",
            "ac/91/20391757b2d867bb372ca6949ea8adaf0040eaad14dc575f350ea1054c02.jpg",
            "fd/7f/02b35fa5c475301ee2a96e987fed058c35cb54bf298c16957bb8ab240118.jpg",
            "94/8f/e52df862cda388561f8fedb620024ce79790b4e3fdca960a7747a38e3a36.jpg",
            "dd/b6/b063cbcf4f8765c7f69b09cbd505a85fb9d79fb4ebfdef8243e2d229a948.jpg",
            "ab/45/0be5ad943216077127c6b0c84bdbb6bb2c20695d4048458e29b68ac061cb.png",
            "ba/be/c790240e2a23790a97885577acbf762a74b3461bafb80ef2bf3076187192.jpeg",
            "ba/be/c790240e2a23790a97885577acbf762a74b3461bafb80ef2bf3076187192.jpeg",
            "ae/d2/861bfbc3ef6ef1527a2ddf187aa7efea3acc0a62cc4efeee28b93234d0f1.jpeg",
        ];

        $client = new Client();
        $dir = formatDateYmd(time());
        foreach ($originalKey as $index => $key){
            $arr = explode(".", $key);
            $suffix = $arr[1];
            $downloadFile = $dir."/".time()."-".random_int(1000, 9999)."-".$index.".{$suffix}";
            $tempData = $client->request("get", picturePath($key))->getBody()->getContents();
            Storage::disk("public")->put($downloadFile, $tempData);
            PrivateFileUtil::uploadFile(
                storage_path("app/public/".$downloadFile),
                $arr[1],
                $key
            );
        }
    }
}
